package com.xindaoapp.happypet.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.api.ConstantsValue;
import com.xindaoapp.happypet.bean.ClubC;
import com.xindaoapp.happypet.bean.ClubCInfo;
import com.xindaoapp.happypet.view.CircleImageView;
import com.xindaoapp.happypet.view.PagerSlidingTabStrip;

/**
 * 我的好友页面
 * 
 * @author Yangqing
 * 
 */
public class MyAttentionActivity extends UMBaseActivity implements
        android.view.View.OnClickListener {
    @ViewInject(R.id.tittle_myattention_cancel)
    private ImageButton tittle_myattention_cancel;
    @ViewInject(R.id.tittle_myattention_ok)
    private TextView tittle_myattention_ok;

    @ViewInject(R.id.club_top)
    private GridView club_top;

    private SharedPreferences sp;

    @ViewInject(R.id.viewpager)
    private ViewPager viewpager;
    private List<GridView> views;

    @ViewInject(R.id.tabs)
    private PagerSlidingTabStrip tabs;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp = getSharedPreferences("menuconfig", MODE_PRIVATE);
        setContentView(R.layout.activity_myattention);
        ViewUtils.inject(this);
        initView();
        initViewPager();
        initGridView();
        initClick();
    }

    private List<ClubC> wxr;
    private List<ClubC> mxr;
    private List<ClubC> sz;
    private List<ClubC> xc;

    private List<ClubC> hr;
    private List<ClubC> location;
    private List<ClubC> all;

    private Adapter wxrAdapter;
    private Adapter mxrAdapter;
    private Adapter szAdapter;
    private Adapter xcAdapter;

    private Adapter hrAdapter;
    private Adapter locationAdapter;
    private Adapter allAdapter;



    /**
     * 初始化GRIDVIEW
     */
    private void initGridView() {
        // ClubInfo info1 = JSON.parseObject(sp.getString("wxr", ""), ClubInfo.class);
        wxr = ConstantsValue.getWxrInfo();
        // ClubInfo info2 = JSON.parseObject(sp.getString("mxr", ""), ClubInfo.class);
        mxr = ConstantsValue.getMxrInfo();
        // ClubInfo info3 = JSON.parseObject(sp.getString("sz", ""), ClubInfo.class);
        sz = ConstantsValue.getSzInfo();
        // ClubInfo info4 = JSON.parseObject(sp.getString("xc", ""), ClubInfo.class);
        xc = ConstantsValue.getXcInfo();

        hr = ConstantsValue.getHrInfo();
        location = ConstantsValue.getLocationInfo();
        all = ConstantsValue.getAllInfo();



        if (top != null && top.size() > 0) {
            wxr.removeAll(top);
            mxr.removeAll(top);
            sz.removeAll(top);
            xc.removeAll(top);
            hr.removeAll(top);
            location.removeAll(top);
            all.removeAll(top);
            club_top.setAdapter(topadapter);
        }
        if (wxr.size() > 0) {
            wxrAdapter = new Adapter(wxr);
            views.get(0).setAdapter(wxrAdapter);
        }

        if (mxr.size() > 0) {
            mxrAdapter = new Adapter(mxr);
            views.get(1).setAdapter(mxrAdapter);
        }
        if (sz.size() > 0) {
            szAdapter = new Adapter(sz);
            views.get(2).setAdapter(szAdapter);
        }
        if (xc.size() > 0) {
            xcAdapter = new Adapter(xc);
            views.get(3).setAdapter(xcAdapter);
        }
        if (hr.size() > 0) {
            hrAdapter = new Adapter(hr);
            views.get(4).setAdapter(hrAdapter);
        }
        if (location.size() > 0) {
            locationAdapter = new Adapter(location);
            views.get(5).setAdapter(locationAdapter);
        }
        if (all.size() > 0) {
            allAdapter = new Adapter(all);
            views.get(6).setAdapter(allAdapter);
        }
        initOnItemVlick();


    }

    /**
     * 设置ITEM的点击事件
     */
    private void initOnItemVlick() {
        views.get(0).setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // Toast.makeText(MyAttentionActivity.this, wxr.get(position).getName() + "增加了",
                // Toast.LENGTH_SHORT).show();
                top.add(wxr.get(position));
                wxr.remove(wxr.get(position));
                if (top != null && top.size() > 0) {
                    club_top.setAdapter(topadapter);
                }
                notifyDataSetChanged();
            }
        });
        views.get(1).setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                top.add(mxr.get(position));
                mxr.remove(mxr.get(position));
                if (top != null && top.size() > 0) {
                    club_top.setAdapter(topadapter);
                }
                notifyDataSetChanged();
            }
        });
        views.get(2).setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                top.add(sz.get(position));
                sz.remove(sz.get(position));
                if (top != null && top.size() > 0) {
                    club_top.setAdapter(topadapter);
                }
                notifyDataSetChanged();
            }
        });
        views.get(3).setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                top.add(xc.get(position));
                xc.remove(xc.get(position));
                if (top != null && top.size() > 0) {
                    club_top.setAdapter(topadapter);
                }
                notifyDataSetChanged();
            }
        });
        views.get(4).setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                top.add(hr.get(position));
                hr.remove(hr.get(position));
                if (top != null && top.size() > 0) {
                    club_top.setAdapter(topadapter);
                }
                notifyDataSetChanged();
            }
        });
        views.get(5).setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                top.add(location.get(position));
                location.remove(location.get(position));
                if (top != null && top.size() > 0) {
                    club_top.setAdapter(topadapter);
                }
                notifyDataSetChanged();
            }
        });
        views.get(6).setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                top.add(all.get(position));
                all.remove(all.get(position));
                if (top != null && top.size() > 0) {
                    club_top.setAdapter(topadapter);
                }
                notifyDataSetChanged();
            }
        });
        club_top.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (top.get(position).getPud().equals(Address.WXR)) {
                    if (wxr.size() == 0) {
                        wxr.add(top.get(position));
                        wxrAdapter = new Adapter(wxr);
                        views.get(0).setAdapter(wxrAdapter);
                    } else {
                        wxr.add(top.get(position));
                        notifyDataSetChanged();
                    }
                }
                if (top.get(position).getPud().equals(Address.MXR)) {
                    if (mxr.size() == 0) {
                        mxr.add(top.get(position));
                        mxrAdapter = new Adapter(mxr);
                        views.get(1).setAdapter(mxrAdapter);
                    } else {
                        mxr.add(top.get(position));
                        notifyDataSetChanged();
                    }
                }
                if (top.get(position).getPud().equals(Address.SZ)) {
                    if (sz.size() == 0) {
                        sz.add(top.get(position));
                        szAdapter = new Adapter(sz);
                        views.get(2).setAdapter(szAdapter);
                    } else {
                        sz.add(top.get(position));
                        notifyDataSetChanged();
                    }
                }
                if (top.get(position).getPud().equals(Address.XC)) {
                    if (xc.size() == 0) {
                        xc.add(top.get(position));
                        xcAdapter = new Adapter(xc);
                        views.get(3).setAdapter(xcAdapter);
                    } else {
                        xc.add(top.get(position));
                        notifyDataSetChanged();
                    }
                }
                if (top.get(position).getPud().equals(Address.HR)) {
                    if (hr.size() == 0) {
                        hr.add(top.get(position));
                        hrAdapter = new Adapter(hr);
                        views.get(4).setAdapter(hrAdapter);
                    } else {
                        hr.add(top.get(position));
                        notifyDataSetChanged();
                    }
                }
                if (top.get(position).getPud().equals(Address.LOCATION)) {
                    if (location.size() == 0) {
                        location.add(top.get(position));
                        locationAdapter = new Adapter(location);
                        views.get(5).setAdapter(locationAdapter);
                    } else {
                        location.add(top.get(position));
                        notifyDataSetChanged();
                    }
                }
                if (top.get(position).getPud().equals(Address.ALL)) {
                    if (all.size() == 0) {
                        location.add(top.get(position));
                        allAdapter = new Adapter(all);
                        views.get(6).setAdapter(allAdapter);
                    } else {
                        all.add(top.get(position));
                        notifyDataSetChanged();
                    }
                }
                top.remove(top.get(position));
                notifyDataSetChanged();
            }
        });
    }

    @SuppressWarnings("static-access")
    private void initViewPager() {
        views = new ArrayList<GridView>();
        viewpager.setAdapter(new MyPagerAdapter(7));
        tabs.setViewPager(viewpager);
        tabs.setIndicatorColor(new Color().rgb(220, 78, 42));
    }

    private List<ClubC> top;
    private TopAdapter topadapter;

    /**
     * 初始化top
     */
    private void initView() {
        top = new ArrayList<ClubC>();
        if (!sp.getString("top", "").equals("")) {
            ClubCInfo topinfo = JSON.parseObject(sp.getString("top", ""), ClubCInfo.class);
            top = topinfo.getArray();
        }
        topadapter = new TopAdapter();
    }

    private class TopAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return top.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(MyAttentionActivity.this, R.layout.left_nemu_list, null);
                holder.iv_item_list = (CircleImageView) convertView.findViewById(R.id.iv_item_list);
                holder.tv_item_list = (TextView) convertView.findViewById(R.id.tv_item_list);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.iv_item_list.setImageResource(top.get(position).getId());
            holder.tv_item_list.setText(top.get(position).getName());
            holder.tv_item_list.setTextColor(android.graphics.Color.BLACK);
            return convertView;
        }
    }

    /**
     * GridView的适配器
     * 
     * @author Yomine
     * 
     */
    private class Adapter extends BaseAdapter {
        private final List<ClubC> clubs;

        public Adapter(List<ClubC> clubs) {
            super();
            this.clubs = clubs;
        }

        @Override
        public int getCount() {
            return clubs.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(MyAttentionActivity.this, R.layout.left_nemu_list, null);
                holder.iv_item_list = (CircleImageView) convertView.findViewById(R.id.iv_item_list);
                holder.tv_item_list = (TextView) convertView.findViewById(R.id.tv_item_list);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.iv_item_list.setImageResource(clubs.get(position).getId());
            holder.tv_item_list.setText(clubs.get(position).getName());
            holder.tv_item_list.setTextColor(android.graphics.Color.BLACK);
            return convertView;
        }
    }

    private class ViewHolder {
        CircleImageView iv_item_list;
        TextView tv_item_list;
    }

    private void initClick() {
        tittle_myattention_cancel.setOnClickListener(this);
        tittle_myattention_ok.setOnClickListener(this);
    }

    /**
     * 保存JSON数据
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tittle_myattention_cancel:
                finish();
                break;
            case R.id.tittle_myattention_ok:
                String jsonString = JSON.toJSONString(top);
                Editor edit = sp.edit();
                edit.putString("top", "{" + "\"array\":" + jsonString + "}");
                edit.commit();
                Toast.makeText(getApplicationContext(), "保存完成", Toast.LENGTH_SHORT).show();
                finish();
                break;
            default:
                break;
        }

    }

    /**
     * pageAdapter
     * 
     * @author Yomine
     * 
     */
    private class MyPagerAdapter extends PagerAdapter {
        private final int count;

        public MyPagerAdapter(int count) {
            super();
            this.count = count;
            for (int i = 0; i < count; i++) {
                GridView gridView = new GridView(MyAttentionActivity.this);
                gridView.setNumColumns(4);
                gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
                gridView.setVerticalSpacing(15);
                views.add(gridView);
            }
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return str[position];
        }

        @Override
        public int getCount() {
            return count;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = views.get(position);
            container.addView(view);
            return view;
        }

    }

    private final String[] str = new String[] {"汪星人", "喵星人", "水星人", "小宠", "火热", "地方", "综合"};

    /**
     * 更新页面
     */
    private void notifyDataSetChanged() {
        if (wxrAdapter != null) {
            wxrAdapter.notifyDataSetChanged();
        }
        if (topadapter != null) {
            topadapter.notifyDataSetChanged();
        }
        if (mxrAdapter != null) {
            mxrAdapter.notifyDataSetChanged();
        }
        if (szAdapter != null) {
            szAdapter.notifyDataSetChanged();
        }
        if (xcAdapter != null) {
            xcAdapter.notifyDataSetChanged();
        }
        if (hrAdapter != null) {
            hrAdapter.notifyDataSetChanged();
        }
        if (locationAdapter != null) {
            locationAdapter.notifyDataSetChanged();
        }
        if (allAdapter != null) {
            allAdapter.notifyDataSetChanged();
        }
    }
}
